package com.yhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhx.iservice.IUserService;
import com.yhx.mapper.UserMapper;
import com.yhx.mapper.UserRoleMapper;
import com.yhx.pojo.User;
import com.yhx.pojo.UserRole;
import com.yhx.utils.PageResult;
import com.yhx.utils.QueryPageBean;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public PageResult listpage(QueryPageBean queryPageBean) {
        QueryWrapper<User> queryWapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        if(queryString!=null&&queryString.length()>0){
            queryWapper.like("username",queryString).or().like("telephone", queryString);
        }
        //创建分页对象
        Page<User> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<User> userPage = userMapper.selectPage(page, queryWapper);

        return new PageResult(userPage.getTotal(), userPage.getRecords());
    }
    @Resource
    private UserRoleMapper userRoleMapper;
    @Override
    public int save(User user) {
        Integer roleid = user.getRoleid();

        if(user.getPassword()==null || user.getPassword()==""||user.getPassword().length()==0){
            user.setPassword("111111");
        }
        int insert = userMapper.insert(user);
        Integer id = user.getId();
        UserRole userRole = new UserRole(id, roleid);
        int insert1 = userRoleMapper.insert(userRole);
        return 1;
    }



    @Override
    public int delete(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        return userMapper.deleteBatchIds(list);
    }

    @Override
    public User getByid(int id) {

        User user = userMapper.selectById(id);
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id", id);
        UserRole userRole = userRoleMapper.selectOne(userRoleQueryWrapper);
        Integer roleId = userRole.getRoleId();
        user.setRoleid(roleId);
        return user;
    }

    @Override
    public int update(User user) {
        Integer id = user.getId();
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id", id);
        int delete = userRoleMapper.delete(userRoleQueryWrapper);
        UserRole userRole = new UserRole(id, user.getRoleid());
        userRoleMapper.insert(userRole);
        return userMapper.updateById(user);
    }

    @Override
    public int deleteOne(int id) {
        return userMapper.deleteById(id);
    }

    @Override
    public User getByUsername(String username) {
        QueryWrapper<User> userQueryWrappey=new QueryWrapper<>();
        userQueryWrappey.eq("username",username);
        return userMapper.selectOne(userQueryWrappey);
    }
}
